package com.organization

import org.springframework.context.i18n.LocaleContextHolder

class CommonService {

    static final Map badResponse = [success: "false"];
    static final Map goodResponse = [success: "true"];

    static final String rootNodeStr = "Organization"
    static final String depNodeStr = "Department_"
    static final String projNodeStr = "Project_"
    static final String emplNodeStr = "Employee_"

    //inject spring bean for generate error messages
    def messageSource

    //convert json string date to java Date
    def convertDateFromJson(date){
        date?(new Date().parse("yyyy-MM-dd", date)):null
    }

    //parse domain erros map to extjs form errors format
    def parseErrorsFromDomain(domainErrors){
        def errors = [:]
        if (domainErrors)
            domainErrors.allErrors.each{errors.putAt(it.field,messageSource.getMessage(it,LocaleContextHolder.locale))}
        //use success: false without quotes! it's important for extjs form, wich would be show errors if logical value of success key is false
        return [success: false, "errors": errors]
    }

    //convert "nodetype_nodeId" to "nodeId"
    def nodeIdToDbId(nodeId){
        nodeId.replaceAll("[a-zA-Z/_]","")
    }

}
